local skill = fk.CreateSkill {
  name = "hanqing__favorable_auspices_skill",
}

Fk:loadTranslationTable{
  ["hanqing__favorable_auspices_skill"] = "大吉大利",
  ["#hanqing__favorable_auspices_skill"] = "所有角色按手速顺序各摸一张牌，你和展示牌点数最大的角色各摸一张牌",
  ["#hanqing__favorable_auspices-choice"] = "大吉大利：拼手速抢红包！",
  ["hanqing__favorable_auspices_draw1"] = "抢！",
  ["hanqing__favorable_auspices_draw2"] = "抢！",
  ["hanqing__favorable_auspices_draw3"] = "抢！",
}

skill:addEffect("cardskill", {
  prompt = "#hanqing__favorable_auspices_skill",
  can_use = Util.GlobalCanUse,
  on_use = function (self, room, cardUseEvent)
    return Util.AoeCardOnUse(self, cardUseEvent.from, cardUseEvent, true)
  end,
  mod_target_filter = Util.TrueFunc,
  on_action = function(self, room, use, finished)
    use.extra_data = use.extra_data or {}
    if not finished then
      use.extra_data.hanqing__favorable_auspices = {}
      local targets = table.simpleClone(use.tos)
      local req = Request:new(targets, "AskForChoice")
      req.focus_text = skill.name
      req.receive_decode = false
      local choices = {
        "hanqing__favorable_auspices_draw1",
        "hanqing__favorable_auspices_draw2",
        "hanqing__favorable_auspices_draw3",
      }
      local data = {
        choices,
        choices,
        skill.name,
        "#hanqing__favorable_auspices-choice",
      }
      for _, p in ipairs(targets) do
        req:setData(p, data)
      end
      req:ask()
      local new_tos = {}
      for _, p in ipairs(req.winners) do
        for _, q in ipairs(use.tos) do
          if q == p then
            table.insert(new_tos, q)
          end
        end
      end
      use.tos = new_tos
    else
      local info = use.extra_data.hanqing__favorable_auspices
      local n = 0
      if info then
        for _, p in ipairs(room:getAlivePlayers()) do
          if not p.dead then
            for _, dat in ipairs(info) do
              if dat[1] == p.id and table.contains(p:getCardIds("h"), dat[2]) then
                n = math.max(n, Fk:getCardById(dat[2]).number)
                p:showCards(dat[2])
              end
            end
          end
        end
      end
      if not use.from.dead then
        use.from:drawCards(1, skill.name)
      end
      for _, p in ipairs(room:getAlivePlayers()) do
        if not p.dead then
          for _, dat in ipairs(info) do
            if dat[1] == p.id and n == Fk:getCardById(dat[2]).number then
              p:drawCards(1, skill.name)
            end
          end
        end
      end
    end
  end,
  on_effect = function(self, room, effect)
    effect.extra_data = effect.extra_data or {}
    effect.extra_data.hanqing__favorable_auspices = effect.extra_data.hanqing__favorable_auspices or {}
    local target = effect.to
    local cards = target:drawCards(1, skill.name)
    if #cards > 0 then
      table.insert(effect.extra_data.hanqing__favorable_auspices, {target.id, cards[1]})
    end
  end,
})

return skill
